﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinFormUI;
using NFLDriver;

public class NFLDriverLogic
{
    //private ComModule cm = new ComModule();
    private Schedule schedule = new Schedule();
    private Teams teams = new Teams();
    private User user = new User();
    //collection of team objects for column A
    //private Team[] columnA = new Team[12];
    //collection of team objects for column B
    //private Team[] columnB = new Team[7];
    //collection of team objects for column C
    //private Team[] columnC = new Team[13];

    Comm comm = new Comm();

    public NFLDriverLogic()
	{
        populateTeams();
        //populateTeamColums();
	}

    public void populateTeams()
    {
        for (int i = 0; i < 32; i++)
        {
            teams.setTeam(i, comm.getTeam(i));
        }

    }

    public void setUserName(String email)
    {
        user.Email = email;
    }
    public void setUserPassword(String password)
    {
        user.Password = password;
    }

    public void setUserTeam(int index, String teamName)
    {
        user.setSelectedTeam(index, teams.getTeam(teamName));
    }

    public void createAccountShow(NFLDriverLogic driverInstance)
    {
        
    }

    //always remember to check if return is null when retrieving objects
    public Team getTeams(int index)
    {
        return teams.getTeam(index);
    }

    //always remember to check if return is null when retrieving objects
    public Team getUserSelectedTeam(int index)
    {
        return user.getSelectedTeam(index);
    }
    public void setUserSelectedTeam(int index, Team team)
    {
        if ((index < 7) && (index >= 0))
        {
            if (team != null)
            {
                user.setSelectedTeam(index, team);
            }
            else
            {
                MessageBox.Show("Null Reference exception at NFLDriverLogic.setUserSelectedTeam(int index, Team team)\team = null", "Exception: Null reference exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        else
        {
            MessageBox.Show("Invalid parameter at NFLDriverLogic.setUserSelectedTeam(int index, Team team)\nindex = " + index, "Exception: Invalid parameter", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    public void setUserSelectedTeam(Team team)
    {
        if (isTeamSelected(team))
        {
            teams.getTeam(team.teamName).isSelected = false;
        }
        else
        {
            if (selectedTeamsCount() < 7)
            {
                teams.getTeam(team.teamName).isSelected = true;
                updateSelectedTeams();
            }
        }

    }

    private void updateSelectedTeams()
    {
        int index = 0;
        for (int i = 0; i < 32; i++)
        {
            if (teams.getTeam(i).isSelected)
            {
                if (index < 7)
                {
                    user.setSelectedTeam(index, teams.getTeam(i));
                    index++;
                }
            }
        }
    }

    private bool isTeamSelected(Team team)
    {
        bool isSelected = false;

        if (teams.getTeam(team.teamName).isSelected)
        {
            isSelected = true;
        }

        return isSelected;
    }

    private bool isTeamSelected(string TeamName)
    {
        bool isSelected = false;

        if (teams.getTeam(TeamName).isSelected)
        {
            isSelected = true;
        }

        return isSelected;
    }

    private int selectedTeamsCount()
    {
        int SelectedTeamsCount = 0;
        for (int i = 0; i < 32; i++)
        {
            if (teams.getTeam(i).isSelected)
            {
                SelectedTeamsCount++;
            }
        }

        return SelectedTeamsCount;
    }

    //checks to see if the password is valid (long enough and has enough complexity)
    public bool isValidPassword(string Password)
    {
        bool validPassword = false;
        //variables that hold the counts of different character types
        int uppercaseCount = 0;
        int lowercaseCount = 0;
        int numberCount = 0;
        int specialCharCount = 0;

        if (Password.Length > 5)
        {
            for (int i = 0; i < Password.Length; i++)
            {
                if (Char.IsUpper(Password[i]))
                {
                    uppercaseCount++;
                }
                if(Char.IsLower(Password[i]))
                {
                    lowercaseCount++;
                }
                if (Char.IsNumber(Password[i]))
                {
                    numberCount++;
                }
                if (Char.IsSymbol(Password[i]))
                {
                    specialCharCount++;
                }
            }
            if (uppercaseCount > 1)
            {
                if (numberCount > 1)
                {
                    if (specialCharCount > 1)
                    {
                        if (lowercaseCount > 1)
                        {
                            validPassword = true;
                        }
                    }
                }
            }
        }

        return validPassword;
    }

    //returns a team object with the specific team name
    public Team getTeam(string teamName)
    {
        if (teams.getTeam(teamName) != null)
        {
            return teams.getTeam(teamName);
        }
        else
        {
            return null;
        }
    }

    public void createAccount(string FirstName, string LastName, string Email, string Password)
    {
        user.FirstName = FirstName;
        user.LastName = LastName;
        user.Email = Email;
        user.Password = Password;
        comm.createUserAccount(user);
    }

    public bool login(string email, string password)
    {
        bool teamsSelected = false;
        User tempUser = new User();
        User returnUser;
        tempUser.Email = email;
        tempUser.Password = password;
        returnUser = comm.userLogin(tempUser);
        if (returnUser.loggedIn())
        {
            user.Email=returnUser.Email;
            user.FirstName=returnUser.FirstName;
            user.LastName=returnUser.LastName;
            user.Password=returnUser.Password;
            user.setLogIn(1);
            //Guid testGuid = new Guid();
            //testGuid=Guid.Empty;
            if (user.getSelectedTeam(0).teamName.Equals(""))
            {
                teamsSelected = false;
            }
            else
            {
                for (int i = 0; i < 7; i++)
                {
                    user.setSelectedTeam(i, teams.getTeam(returnUser.getSelectedTeam(i).teamName));
                    user.getSelectedTeam(i).teamColumn = teams.getTeam(user.getSelectedTeam(i).teamName).teamColumn;
                }
                teamsSelected = true;
            }
        }
        return teamsSelected;
    }
    /*
    private void populateTeamColums()
    {
        for (int i = 0; i < 32; i++)
        {
            if (i < 12)
            {
                columnA[i] = cm.getTeamName(i);
            }
            else
            {
                if (i < 19)
                {
                    columnC[i] = cm.getTeamName(i);
                }
                else
                {
                    columnC[i] = cm.getTeamName(i);
                }
            }
        }

    }
*/
    //returns team at columnNumber and columnIndex number.  ColumnNumber corresponds to the listBoxes in the main form (A=0, B=1, C=2) and columnIndex corresponds to the team in that location on the listbox.
    /*
    public Team getTeam(int columnNumber, int columnIndex)
    {
        switch (columnNumber)
        {
            case 0:
                {
                    if (columnIndex > 11)
                    {
                        MessageBox.Show("Invalid columnIndex value at NFLDriver.getTeam()\ncolumnIndex = " + columnIndex, "Exception: Invalid input", MessageBoxButtons.OK);
                    }
                    else
                    {
                        return columnA[columnIndex];
                    }
                    break;
                }
            case 1:
                {
                    if (columnIndex > 6)
                    {
                        MessageBox.Show("Invalid columnIndex value at NFLDriver.getTeam()\ncolumnIndex = " + columnIndex, "Exception: Invalid input", MessageBoxButtons.OK);
                    }
                    else
                    {
                        return columnC[columnIndex];
                    }
                    break;
                }
            case 2:
                {
                    if (columnIndex > 12)
                    {
                        MessageBox.Show("Invalid columnIndex value at NFLDriver.getTeam()\ncolumnIndex = " + columnIndex, "Exception: Invalid input", MessageBoxButtons.OK);
                    }
                    else
                    {
                        return columnC[columnIndex];
                    }
                    break;
                }
            default:
                {
                    MessageBox.Show("Invalid columnNumber value at NFLDriver.getTeam()\ncolumnNumber = " + columnNumber, "Exception: Invalid input", MessageBoxButtons.OK);
                    //return null;
                    break;
                }
        }
        return null;
    }
    */



}
